//
//  QLMDeviceDetailInfoVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/10/12.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMDeviceDetailInfoVC.h"
#import "QLMDeviceInfoTabCell.h"

#import "QLMPhotoInfoTabCell.h"

@interface QLMDeviceDetailInfoVC ()<UITableViewDelegate,UITableViewDataSource,QLMPhotoInfoTabCellDelegate>{
    NSArray *tipsArray;
    
    NSMutableArray *selectedPhotos;
    QLMDeviceInfoTabCell *deviceInfoCell;
    
    NSArray *tipSArray;
    NSArray *valueSArray;
}

@end

@implementation QLMDeviceDetailInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"设备显示";
    
    selectedPhotos=[NSMutableArray array];
    tipsArray=[NSArray arrayWithObjects:@"基本信息",@"机主信息",@"其他信息", nil];
    NSArray *tips1Array=[NSArray arrayWithObjects:@"产品",@"机号",@"别名",@"出厂日期", nil];
    NSArray *tips2Array=[NSArray arrayWithObjects:@"机主",@"电话", nil];
    NSArray *tips3Array=[NSArray arrayWithObjects:@"备注",@"照片", nil];
    
    tipSArray=@[tips1Array,tips2Array,tips3Array];
    
    NSString *deviceNameStr=[NSString stringWithFormat:@"%@%@",_deviceInfoModel.brandName,_deviceInfoModel.modelName];
    NSString *seriNoStr=[self resultString:_deviceInfoModel.serialNo];
    NSString *otherName=[self resultString:_deviceInfoModel.alias];
    NSString *productionDate=[self resultString:_deviceInfoModel.productionDate];
    NSString *remarkStr=[self resultString:_deviceInfoModel.remark];
    
    NSArray *value1Array=[NSArray arrayWithObjects:deviceNameStr,seriNoStr,otherName,productionDate, nil];
    NSArray *value2Array=[NSArray arrayWithObjects:_deviceInfoModel.mainMachineName,_deviceInfoModel.mainMachinePhone, nil];
    NSArray *value3Array=[NSArray arrayWithObjects:remarkStr,nil];
    
    valueSArray=@[value1Array,value2Array,value3Array];
    
    UITableView *_otableview=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _otableview.backgroundColor=[UIColor clearColor];
    _otableview.delegate=self;
    _otableview.dataSource=self;
    _otableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    _otableview.showsVerticalScrollIndicator=NO;
    _otableview.showsHorizontalScrollIndicator=NO;
    _otableview.tableFooterView=[UIView new];
    [self.view addSubview:_otableview];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self _setSelectedPhotos];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_setSelectedPhotos{
    for (QLMDevicePicModel *picDetailModel in self.deviceInfoModel.pictureDetail) {
        NSString *ulrStr=[NSString stringWithFormat:@"%@%@",kBaseURL,picDetailModel.pathName];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:ulrStr]];
        UIImage *aimage = [UIImage imageWithData:data];
        if (aimage!=nil) {
            [selectedPhotos addObject:aimage];
        }
    }
}

- (NSString *)resultString:(NSString *)infoStr{
    NSString *resultStr=@"无";
    if (infoStr.length!=0&&[ClientTool isNull:infoStr]) {
        resultStr=infoStr;
    }
    return resultStr;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UITableView datasource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView=[[UIView alloc] init];
    headerView.backgroundColor=[UIColor whiteColor];
    
    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
    newButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    newButton.frame = CGRectMake(10, 5, 100, 34);
    [newButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
    [newButton setTitle:tipsArray[section] forState:UIControlStateNormal];
    [newButton setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
    newButton.titleLabel.font=[UIFont systemFontOfSize:16.0f];
    newButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    newButton.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
    [headerView addSubview:newButton];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView=[[UIView alloc] init];
    footerView.backgroundColor=[UIColor clearColor];
    return footerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 4;
    }
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==2) {
        if (indexPath.row==1) {
            if (selectedPhotos.count==0) {
                return 34;
            }
            return 130;
        }else{
            return [deviceInfoCell autoChangeHeightWithLabel:deviceInfoCell.nameInfoLabel]+10+5;
        }
    }
    return 34;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==2) {
        if (indexPath.row==1) {
            QLMPhotoInfoTabCell *photocell=[QLMPhotoInfoTabCell cellWithTableView:tableView];
            photocell.delegate=self;
            photocell.isCanEditable=NO;
            photocell.selectedPhotos=selectedPhotos;
            photocell.selectionStyle=UITableViewCellSelectionStyleNone;
            
            if (selectedPhotos.count==0) {
                UILabel *_tiplabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 0, kScreen_width-105-20,34)];
                _tiplabel.text = @"无";
                _tiplabel.textColor=[UIColor darkGrayColor];
                _tiplabel.backgroundColor=[UIColor clearColor];
                _tiplabel.font = [UIFont systemFontOfSize:16];
                _tiplabel.textAlignment = NSTextAlignmentLeft;
                [photocell addSubview:_tiplabel];
            }
            
            return photocell;
        }
    }
    deviceInfoCell=[QLMDeviceInfoTabCell cellWithTableView:tableView];
    deviceInfoCell.tipsInfoLabel.text=tipSArray[indexPath.section][indexPath.row];
    deviceInfoCell.nameInfoLabel.text=valueSArray[indexPath.section][indexPath.row];
    return deviceInfoCell;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)tapPreViewerAction:(NSInteger)itemTag{//预览照片
    [XLPhotoBrowser showPhotoBrowserWithImages:selectedPhotos currentImageIndex:itemTag];
}

@end
